﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Controller;
using RM2012Server.Model;

namespace RM2012Server.View
{
    public partial class frmInvoice : Office2007Form
    {
        InvoiceCtrl m_InvoiceCtrl = new InvoiceCtrl();
        InvoiceDetailCtrl m_InvoiceDetailCtrl = new InvoiceDetailCtrl();

        public frmInvoice()
        {
            InitializeComponent();
        }

        private void frmInvoice_Load(object sender, EventArgs e)
        {
            DateTime dtNow = DateTime.Now;
            dtChoise.Value = dtNow;
            RefreshGrid();
        }

        private void btnDetail_Click(object sender, EventArgs e)
        {
            int rowIndex = dgvInvoice.CurrentCell.RowIndex;
            String invoiceId = dgvInvoice.Rows[rowIndex].Cells[0].Value.ToString();
            frmInvoiceDetail frm = new frmInvoiceDetail();
            frm.Text = invoiceId + " - " + frm.Text;
            frm.ShowDialog();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        public void RefreshGrid()
        {
            try
            {
                DataTable dtInvoiceAll = m_InvoiceCtrl.GetAllFull();
                dgvInvoice.DataSource = dtInvoiceAll;
                dgvInvoice.Refresh();
            }
            catch
            {
            	
            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime dateChoise = dtChoise.Value;
                DataTable datatableSource = m_InvoiceCtrl.GetByDateFull(dateChoise);
                dgvInvoice.DataSource = datatableSource;
                dgvInvoice.Refresh();
            }
            catch
            {
                MessageBoxEx.Show("Chọn ngày trước khi xem !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int rowIndex = dgvInvoice.CurrentCell.RowIndex;
                String invoiceId = dgvInvoice.Rows[rowIndex].Cells[0].Value.ToString();
                if (MessageBoxEx.Show("Bạn có chắc chắn xóa ?", "XÓA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (m_InvoiceDetailCtrl.DeleteByInvoice(invoiceId) > 0)
                    {
                        if (m_InvoiceCtrl.Delete(invoiceId) > 0)
                        {
                            RefreshGrid();
                        }
                    }
                }
            }
            catch
            {
            	
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            int rowIndex = dgvInvoice.CurrentCell.RowIndex;
            String invoiceId = dgvInvoice.Rows[rowIndex].Cells[0].Value.ToString();
            GeneralInfo.invoiceIdReport = invoiceId;
            frptInvoice frm = new frptInvoice();
            frm.ShowDialog();
        }
    }
}
